



//
//  ZRootViewController.m
//  ZQLearn
//
//  Created by 朵 on 2019/4/16.
//  Copyright © 2019年 admin. All rights reserved.
//

#import "ZRootViewController.h"
#import "ZLearnViewController.h"
#import "ZRMediaViewController.h"
#import "ZHomeViewController.h"
#import "ZLibViewController.h"
#import "ZMineViewController.h"
#import "ZNavigationViewController.h"

@interface ZRootViewController ()
@property (nonatomic,strong)ZRMediaViewController *media;
@property (nonatomic,strong)ZLearnViewController *read;
@property (nonatomic,strong)ZHomeViewController *home;
@property (nonatomic,strong)ZLibViewController *lib;
@property (nonatomic,strong)ZMineViewController *mine;
@end

@implementation ZRootViewController

-(ZRMediaViewController *)media{
    
    if (!_media) {
        _media = [[ZRMediaViewController alloc] init];
        _media.title = @"融媒体";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _media.tabBarItem=s_bar;
    }
    return _media;
}

-(ZLearnViewController *)read{
    if (!_read) {
        _read = [[ZLearnViewController alloc] init];
        _read.title = @"读书";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _read.tabBarItem=s_bar;
    }
    return _read;
}

-(ZHomeViewController *)home{
    if (!_home) {
        _home = [[ZHomeViewController alloc] init];
        _home.title = @"";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _home.tabBarItem=s_bar;
    }
    return _home;
}

-(ZLibViewController *)lib{
    if (!_lib) {
        _lib = [[ZLibViewController alloc] init];
        _lib.title = @"图书馆";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _lib.tabBarItem=s_bar;
    }
    return _lib;
}

- (ZMineViewController *)mine{
    if (!_mine) {
        _mine = [[ZMineViewController alloc] init];
        _mine.title = @"我的";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _mine.tabBarItem=s_bar;
    }
    return _mine;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTabBarVC];
    // Do any additional setup after loading the view.
}

-(void)configTabBarVC{
    
    
    NSArray * titleArray;
    NSArray * unSelectImageArray;
    NSArray * selectImageArray;
    //登录用户类型
    //userdefault  是 永久性存，单例是跟随着整个程序的生命周期，所以结合起来用更好
    NSString *uid = [[NSUserDefaults standardUserDefaults]objectForKey:kUserId];
    if (uid.length>2) {
        [UserInfoSave shareInstance].uid = uid;
    }
    //1企业   0个人
    ZNavigationViewController * nav0 = [[ZNavigationViewController alloc] initWithRootViewController:self.media];
    ZNavigationViewController * nav1 = [[ZNavigationViewController alloc] initWithRootViewController:self.read];
    ZNavigationViewController * nav2 = [[ZNavigationViewController alloc] initWithRootViewController:self.home];
    ZNavigationViewController * nav3 = [[ZNavigationViewController alloc] initWithRootViewController:self.lib];
    ZNavigationViewController * nav4 = [[ZNavigationViewController alloc] initWithRootViewController:self.mine];
    
    self.viewControllers = @[nav0,nav1,nav2,nav3,nav4];
    
    titleArray = @[@"融媒体",
                   @"读书",
                   @"首页",
                   @"图书馆",
                   @"我的"];
    
    unSelectImageArray = @[@"rx",
                           @"dx",
                           @"sx",
                           @"tx",
                           @"wx"];
    selectImageArray = @[@"rd",
                         @"dd",
                         @"sd",
                         @"td",
                         @"wd"];
    
    for (int i=0; i < unSelectImageArray.count; i++) {
        
        NSString * titel = titleArray[i];
        
        UIImage * normal_image = [UIImage imageNamed:unSelectImageArray[i]];
        normal_image = [normal_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIImage * selected_image = [UIImage imageNamed:selectImageArray[i]];
        //取消系统对于选中 item 的渲染
        selected_image = [selected_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //获取每一个 item
        UITabBarItem *item=[self.tabBar.items objectAtIndex:i];
        
        item = [item initWithTitle:titel
                             image:normal_image
                     selectedImage:selected_image];
        
        //item.imageInsets = UIEdgeInsetsMake(5, 5, -5, -5);
        [item setTitlePositionAdjustment:UIOffsetMake(0, -2)];
        
    }
    
    //设置 tabbar 字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.099 alpha:1.000], NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateNormal];
    //字体颜色变化
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:NColorRGB(108.f, 166.f, 248.f), NSFontAttributeName:[UIFont systemFontOfSize:10]} forState:UIControlStateSelected];
    
    //    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbarBackground"];
    self.selectedIndex = 2;
}


//后期作为优化点
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
//    NSInteger index = [self.tabBar.items indexOfObject:item];
//    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:0]} forState:UIControlStateSelected];
//
//    for (UIView *tabBarButton in self.tabBar.subviews) {
    
        
//        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
//
//        }
//    }
}

@end
